import Vue from 'vue'
// 1、引入路由
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import indexPage from "@/views/indexPage.vue";
import brandManagement from "@/views/brandManagement.vue";
import keyBoardModifiers from "@/views/keyBoardModifiers.vue";
import filterPage from "@/views/filterPage.vue";
import customDirectives from "@/views/customDirectives.vue";
import lifeCycle from "@/views/lifeCycle.vue";
import animatePage from "@/views/animatePage.vue";
import axiosPage from "@/views/axiosPage.vue";
import fatherPage from "@/views/fatherPage.vue";
import courseIndex from "@/views/course/courseIndex.vue";
import minePage from "@/layOut/minePage.vue";
import pageB from "@/views/pageB.vue";
import pageA from "@/views/pageA.vue";
import antPage from "@/views/antPage.vue";
import personalCenter from "@/views/personalCenter.vue";
import calculateTheListener from "@/views/calculateTheListener.vue";
import shoppingCart from "@/views/shoppingCart.vue";
import vueX from "@/views/vueX.vue";
import mixinPge from "@/views/mixinPge.vue";
import store from "@/store";
import {viewPersonalInformation} from "@/api/http";
//自动导入  @代表src

Vue.use(VueRouter)


// 3、创建映射关系
const routes = [
  /*// 路由重定向
  {
      path: '',
      redirect: '/father'
  },*/


  {
    path: '/',             //路径   名字可以自定义
    name: 'home',          //路由的名字
    component: HomeView    //component  组件
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path:'/index',
    component: indexPage
  },
  {
    path:'/brand',
    component: brandManagement
  },
  {
    path:'/key',
    component: keyBoardModifiers
  },
  {
    path:'/filter',
    component: filterPage,

    // 路由嵌套
    children: [
      {
        // 在路由嵌套的子路由中不带 /
        path: 'animate',
        component: animatePage,
        children: [
          {
            path: 'father',
            component: fatherPage
          }
        ]
      },
      {
        path: 'course',
        // component: courseIndex
        // 视图命名
        components: {
          // courseIndex,
          default:courseIndex,
          keyBoardModifiers,
          brandManagement
        }
      }
    ]

  },
  {
    path:'/directive',
    component: customDirectives
  },
  {
    path:'/life',
    component: lifeCycle
  },
  {
    path:'/animate',
    component: animatePage
  },
  {
    path:'/api',
    component:axiosPage
  },
  {
    path:'/father',
    component:fatherPage
  },
  {
    path: '/course',
    component: courseIndex
  },
  {
    path: '/mine',
    component: minePage,
    children: [
      {
        path: 'course',
        component: courseIndex
      }, {
        path: 'brand',
        component: brandManagement
      }
    ]
  },
  {
    path: '/pageA',
    component: pageA
  },
  {
    // 声明式（2）name params    需要在router文件中写name和path后边要写(更安全)
    // path: '/pageB/:id',
    // path: '/pageB/:sex/:num',
    // name:'pageB',

    path:'/pageB',
    component: pageB
  },
  {
    path: '/ant',
    component: antPage
  },
  {
    path: '/person',
    component: personalCenter
  },
  {
    path: '/cw',
    component: calculateTheListener
  },
  {
    path: '/car',
    component: shoppingCart
  },
  {
    path: '/vueX',
    component: vueX
  },
  {
    path: '/mixin',
    component: mixinPge
  },

]


//2、创建路由实例
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  //   routes:routes


//     高亮自定义类名
  linkActiveClass: 'activeLink',

})


// to:要跳转的路由
// from：从哪个路由跳转
// next：必须要跳转一次 函数
router.beforeEach((to, from, next) => {
  // console.log(to.path)
  console.log(to)
  console.log(from)
  console.log(next)
  // next()
  if (to.path === '/person') {
    console.log(store.state.user.isLogin)
    // if (store.state.user.isLogin) {
    viewPersonalInformation().then(res => {
      console.log(res)
      if (res.code === 0) {
        next()
      } else {
        next('/course')
        // return false
      }
    })
        // next()
    // } else {
    //     next('/brand')
    //     // return false
    // }
  } else {
    next()
  }
})


export default router
